/*
 * @Author: songxiang songxiang0806@qq.com
 * @Date: 2025-08-26 20:25:52
 * @LastEditors: songxiang songxiang0806@qq.com
 * @LastEditTime: 2025-09-03 18:15:00
 * @FilePath: \sdc-supermap-webgl\src\components\viewshed\use-veiwshed.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { onBeforeUnmount, onMounted, reactive, watch } from 'vue'
import { supermapReady } from '/@packages/components'
import { ref } from 'vue'
import { Tooltip, selectCursor, defaultCursor } from '/@src/utils'

export default function () {
  const { $myapp } = supermapReady()
  onMounted(() => {
    init()
  })
  onBeforeUnmount(() => {
    onClear()
  })
  // 可视域分析标识
  const viewshedFlag = ref<'start' | 'doing' | 'done' | 'none'>('none')
  // 初始化可视域分析
  let viewshed3D = new SuperMap3D.ViewShed3D($myapp.viewer.scene)
  // 鼠标提示
  const tooltip = ref<Tooltip>()
  // 鼠标事件注册，可用于注册鼠标移动，鼠标右键等事件
  const mouseHandler = new SuperMap3D.ScreenSpaceEventHandler(
    $myapp.viewer.scene.canvas
  )
  // 注册鼠标移动事件，当已经绘制了观察点后，可进行移动控制可视域分析距离、方向等参数
  mouseHandler.setInputAction(async (e: any) => {
    if (viewshedFlag.value === 'start') {
      tooltip.value?.changePosition(e)
    } else if (viewshedFlag.value === 'doing') {
      tooltip.value?.changePosition(e)
      tooltip.value?.changeMessage('鼠标右键结束可视域分析')
      const position = await $myapp.viewer.scene.pickPositionAsync(
        e.endPosition
      )
      //计算该点与视口位置点坐标的距离
      const dis = SuperMap3D.Cartesian3.distance(
        SuperMap3D.Cartesian3.fromDegrees(
          viewshed3D.viewPosition[0],
          viewshed3D.viewPosition[1],
          viewshed3D.viewPosition[2]
        ),
        position
      )
      if (dis > 0) {
        // 将鼠标当前点坐标转化成经纬度
        const cartographic = SuperMap3D.Cartographic.fromCartesian(position)
        const longitude = SuperMap3D.Math.toDegrees(cartographic.longitude)
        const latitude = SuperMap3D.Math.toDegrees(cartographic.latitude)
        const height = cartographic.height
        // 通过该点设置可视域分析对象的距离及方向
        viewshed3D.setDistDirByPoint([longitude, latitude, height])
      }
    }
  }, SuperMap3D.ScreenSpaceEventType.MOUSE_MOVE)
  // 注册鼠标右键事件，结束可视域分析，并将当前可视域分析属性赋予可视化界面
  mouseHandler.setInputAction((e: any) => {
    if (viewshedFlag.value === 'doing') {
      defaultCursor($myapp.viewer)
      tooltip.value?.destroy()
      viewshedOptions.direction = viewshed3D.direction
      viewshedOptions.pitch = viewshed3D.pitch
      viewshedOptions.distance = viewshed3D.distance
      viewshedOptions.horizontalFov = viewshed3D.horizontalFov
      viewshedOptions.verticalFov = viewshed3D.verticalFov
      viewshedOptions.visibleColor =
        viewshed3D.visibleAreaColor.toCssColorString()
      viewshedOptions.hideColor = viewshed3D.hiddenAreaColor.toCssColorString()
      viewshedFlag.value = 'done'
    }
  }, SuperMap3D.ScreenSpaceEventType.RIGHT_CLICK)
  // 注册鼠标左键事件，绘制可视域观察点
  mouseHandler.setInputAction(async (e: any) => {
    if (viewshedFlag.value === 'start') {
      //获取点击位置笛卡尔坐标
      const position = await $myapp.viewer.scene.pickPositionAsync(e.position)
      var cartographic = SuperMap3D.Cartographic.fromCartesian(position)
      var longitude = SuperMap3D.Math.toDegrees(cartographic.longitude)
      var latitude = SuperMap3D.Math.toDegrees(cartographic.latitude)
      var height = cartographic.height + 0.5
      // 设置视口位置
      viewshed3D.viewPosition = [longitude, latitude, height]
      viewshed3D.build()
      // 将标记置为false以激活鼠标移动回调里面的设置可视域操作
      viewshedFlag.value = 'doing'
    }
  }, SuperMap3D.ScreenSpaceEventType.LEFT_CLICK)

  // 初始化
  const init = () => {}
  // 可视域参数
  const viewshedOptions = reactive({
    direction: 0,
    pitch: 0,
    distance: 0,
    horizontalFov: 0,
    verticalFov: 0,
    visibleColor: '#409EFF',
    hideColor: '#409EFF'
  })

  watch(
    () => viewshedOptions,
    () => {
      if (viewshedFlag.value === 'done') {
        viewshed3D.direction = parseFloat(viewshedOptions.direction.toString())
        viewshed3D.pitch = parseFloat(viewshedOptions.pitch.toString())
        viewshed3D.distance = parseFloat(viewshedOptions.distance.toString())
        viewshed3D.horizontalFov = parseFloat(
          viewshedOptions.horizontalFov.toString()
        )
        viewshed3D.verticalFov = parseFloat(
          viewshedOptions.verticalFov.toString()
        )
        viewshed3D.visibleAreaColor = SuperMap3D.Color.fromCssColorString(
          viewshedOptions.visibleColor
        )
        viewshed3D.hiddenAreaColor = SuperMap3D.Color.fromCssColorString(
          viewshedOptions.hideColor
        )
      }
    },
    {
      deep: true
    }
  )

  // 点击开始绘制可视域分析
  const onStart = () => {
    viewshed3D = new SuperMap3D.ViewShed3D($myapp.viewer.scene)
    tooltip.value = new Tooltip(
      '鼠标左键点击绘制观察点',
      $myapp.viewer.canvas.parentElement
    )
    selectCursor($myapp.viewer)
    viewshedFlag.value = 'start'
  }
  // 点击取消可视域分析
  const onClear = () => {
    viewshedFlag.value = 'none'
    viewshed3D.distance = 0.1
    viewshedOptions.direction = 0
    viewshedOptions.pitch = 0
    viewshedOptions.distance = 0
    viewshedOptions.horizontalFov = 0
    viewshedOptions.verticalFov = 0
    viewshedOptions.visibleColor = ''
    viewshedOptions.hideColor = ''
  }

  return { viewshedOptions, viewshedFlag, onStart, onClear }
}
